package com.lyeducation.controllers.order;

import com.lyeducation.controllers.dto.OrderDTO;
import com.lyeducation.converter.OrderForm2OrderDTOConverter;
import com.lyeducation.enums.ResultEnum;
import com.lyeducation.exception.LeadingEagleException;
import com.lyeducation.form.OrderForm;
import com.lyeducation.services.order.UserOrderServiceImpl;
import com.lyeducation.services.pay.PayService;
import com.lyeducation.util.ResultUtil;
import com.lyeducation.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderController
 * @Description
 * @Author Sky1in3
 * @Date 2019/1/21 17:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/user_order/{mid}")
@Slf4j
@Transactional(rollbackFor = Exception.class, readOnly = false, propagation = Propagation.REQUIRED)
public class UserOrderController {

    @Autowired
    private UserOrderServiceImpl userOrderService;

    @Autowired
    private PayService payService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid @RequestBody OrderForm orderForm,
                                                BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new LeadingEagleException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空！！！");
            throw new LeadingEagleException(ResultEnum.CART_EMPTY);
        }

        OrderDTO createResult = userOrderService.create(orderDTO);

        Map<String, Object> map = new HashMap<>();
        map.put("orderMaster", createResult);

        return ResultUtil.success(map);
    }

    /**
     * 用户订单列表
     */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {

        return ResultUtil.success(userOrderService.findList(openid, new PageRequest(page, size)).getContent());
    }

    /**
     * 用户订单详情
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId) {
        return ResultUtil.success(userOrderService.findOne(openid, orderId));
    }

    /**
     * 用户取消订单
     */
    @GetMapping("/cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId) {
        return ResultUtil.success(userOrderService.cancel(openid, orderId));
    }

    /**
     * 用户完结订单
     */
    @GetMapping("/finish")
    public ResultVO<OrderDTO> finish(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId) {
        return ResultUtil.success(userOrderService.finish(openid, orderId));
    }

    /**
     * 用户发起订单支付
     */
    @PostMapping("/orderPay")
    public ResultVO<OrderDTO> paid(@RequestBody OrderDTO orderDTO) {
        return ResultUtil.success(payService.create(orderDTO).getData());
    }

}
